package Tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Client.PokerClient.src.Client;
import Client.PokerClient.src.GameController;
import Client.PokerClient.src.GameThread;
import Client.PokerClient.src.GameWindow;

public class GameControllerTest {
  GameController[] gameController;
  GameThread gameThread;
  GameWindow gameWindow;
  Client client;
  static Server server;
  
  @BeforeClass
  public static void BServer() {
    server = new Server(4444);
  }
  
  @AfterClass
  public static void AServer() {
    server.EndTest();
  }
  
  @Before
  public void Inicjuj() {
	client = new Client(true);
	client.CreateGame(2, 2, 2, true, false);
	gameController = new GameController[3];
	gameController[0] = new GameController(2, 2, 2, true, false, client);
	gameController[1] = new GameController(2, 1, 3, true, true, client);
	gameController[2] = new GameController(2, 0, 4, true, true, client);
	client.getGameThread().ConnectToServer("127.0.0.1", "4444");
  }
  
  @Test
  public void NOTNULL() {
	for(int i=0;i<gameController.length;i++) {
      assertNotNull(gameController[i].getMaxPlayers());
      assertNotNull(gameController[i].getReplaceCards());
      assertNotNull(gameController[i].getPlayersDecks());
      assertNotNull(gameController[i].getPlayersID());
	}
  }
  
  @Test
  public void Equals() {
	for(int i=0;i<gameController.length;i++) {
      assertEquals(gameController[i].getMaxPlayers(), i+2);
      assertEquals(gameController[i].getActualNumberOfPlayers(), 2);
      assertEquals(gameController[i].getNumberOfBot(), 2-i);
      for(int j=0; j<5; j++)
        assertEquals(gameController[i].getReplaceCards()[j],0);
	}
  }
  
  @Test
  public void S_CARDS() {
	int[][] PlayerDeck = {{12,11,10,9,8}, {0,0,0,0,0}};
    client.getGameController().S_CARDS(PlayerDeck);
    int[][] tmp = client.getGameController().getPlayersDecks()[0];
    for(int i=0; i<PlayerDeck[0].length;i++) {
      assertEquals(tmp[0][i], PlayerDeck[0][i]);
      assertEquals(tmp[1][i], PlayerDeck[1][i]);
    }
    assertTrue(client.getGameWindow().getTimeAndWiner().isVisible());
  }
  
  @Test
  public void S_ID() {
    for(int i=0; i<10;i++) {
      client.getGameController().S_ID(Integer.toString(i));
      assertEquals(client.getGameController().getPlayersID()[0], Integer.toString(i));
    }
  }
  
  @Test
  public void S_PLAYERJOIN() {
    client.CreateGame(4, 0, 4, true, true);
	for(int i=0; i<4;i++) {
      client.getGameController().G_PLAYERJOIN(i);
      assertEquals(client.getGameController().getActualNumberOfPlayers(), i);
	}
  }
  
  @Test
  public void S_PLAYEROUT() {
	client.CreateGame(4, 0, 4, true, true);
    for(int i=0; i<4;i++) {
      client.getGameController().G_PLAYEROUT(i);
      assertEquals(client.getGameController().getActualNumberOfPlayers(), i);
	}
  }
  
  @Test
  public void G_WYMIANA() {
    
  }
  
  @Test
  public void G_ALL() {
	client.getGameController().S_ID("0");
	String[] PlayersID = {"0", "1", "2", "3"};
	int[][][] PlayersDecks = {{{12,11,10,9,8}, {0,0,0,0,0}},
	                          {{12,11,10,9,8}, {1,1,1,1,1}},
	                          {{12,11,10,9,8}, {2,2,2,2,2}},
	                          {{12,11,10,9,8}, {3,3,3,3,3}}};
    client.getGameController().G_ALL(PlayersID, PlayersDecks, "0");
    int[][][] tmp = client.getGameController().getPlayersDecks();
    
    for(int i=0; i<tmp.length; i++)
      for(int j=0; j<tmp[i][0].length; j++) {
    	assertEquals(tmp[i][0][j], PlayersDecks[i][0][j]);
    	assertEquals(tmp[i][1][j], PlayersDecks[i][1][j]);
      }
  }
  
  @Test
  public void G_NEW() {
    client.getGameController().G_NEW();
	int[][][] PlayersDecks = client.getGameController().getPlayersDecks();
    for(int i=0;i<PlayersDecks.length;i++)
      for(int j=0;j<PlayersDecks[i][0].length;j++) {
        assertEquals(PlayersDecks[i][0][j], 0);
        assertEquals(PlayersDecks[i][1][j], 4);
  	  }
  }
  
  @After
  public void Clean() {
	client.getGameThread().finalize();
	client = null;
	gameController = null;
  }
}
